## @package vecLib
# This module is designed to get Location from google gears API based on CellID or Mac
# Main class of Geo, Call Google Gears API to get geo location and address
# All of the functions will return an array with location informations

#coding=utf-8
import httplib, urllib

class Geo:
    ## Get location from cell tower info.
    # @param lac ( @c string) location area code 
    # @param mnc ( @c string) mobile network code, China Mobile is 0, China Unicom is 1 
    # @param cellId ( @c string) Cell ID
    # @param mcc ( @c string) Mobile Country Code , China is 460 
    # @return location info(@c array) a json string contains location info
    def getCellLocation(self, lac, mnc, cellId, mcc):
        self.headers = {"Content-type": "application/jsonrequest"}
        self.conn = httplib.HTTPConnection("www.google.com")
        data = '{"cell_towers": [{"location_area_code": "'+lac+'", "mobile_network_code": "'+mnc+'", "cell_id": "'+cellId+'", "mobile_country_code": "'+mcc+'"}], "version": "1.1.0", "request_address": "true", "address_language": "zh_CN"}'
        self.conn.request("POST", "/loc/json", data, self.headers)
        print 'Requesting location...'
        response = self.conn.getresponse()
        if response.status == 200:

            res = response.read()
            if len(res) == 2:
                print "Invaild Paraments, Location is not detected"
            else:
                return res.decode("utf-8")
                '''
                locationDict = eval(res)
                locationInfo = {'latitude':locationDict['location']['latitude'], 'longitude':locationDict['location']['longitude'], 'accuracy':locationDict['location']['accuracy'], 'country':locationDict['location']['address']['country'], 'region':locationDict['location']['address']['region'], 'city':locationDict['location']['address']['city'], 'street':locationDict['location']['address']['street']}
                return locationInfo
                '''
        else:
            print response.status, response.reason

    ## Get location from MAC.
    # @param mac ( @c string ) Mac
    # @return location info(@c array) a json string contains location info
    def getMacLocation(self, mac):
        self.headers = {"Content-type": "application/jsonrequest"}
        self.conn = httplib.HTTPConnection("www.google.com")
        data = '{"wifi_towers":[{"mac_address": "'+mac+'"}], "version": "1.1.0", "request_address":"true", "address_language": "zh_CN"}'
        self.conn.request("POST", "/loc/json", data, self.headers)
        print 'Requesting location...'
        response = self.conn.getresponse()
        if response.status == 200:

            res = response.read()
            if len(res) == 2:
                print "Invaild Paraments, Location is not detected"
            else:
                return res.decode("utf-8")

                '''
                locationDict = eval(res)
                locationInfo = {'latitude':locationDict['location']['latitude'], 'longitude':locationDict['location']['longitude'], 'accuracy':locationDict['location']['accuracy'], 'country':locationDict['location']['address']['country'], 'region':locationDict['location']['address']['region'], 'city':locationDict['location']['address']['city'], 'street':locationDict['location']['address']['street']}
                return locationInfo
            '''
        else:
            print response.status, response.reason

    def __resolveJason(self, data):
        #use this function to resolve jason files
        pass




            



